package org.jmhome.blog.service.impl;

import java.util.List;

import org.jmhome.blog.model.Category;
import org.jmhome.blog.service.AbstractJpaDaoService;
import org.jmhome.blog.service.CategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service("categoryService")
public class CategoryServiceImpl extends AbstractJpaDaoService implements
		CategoryService {

	/**
	 * @see org.jmhome.blog.service.CategoryService#insertCategory(org.jmhome.blog.model.Category)
	 */
	@Override
	public void insertCategory(Category category) {
		getJpaTemplate().persist(category);

	}

	/**
	 * @see org.jmhome.blog.service.CategoryService#updateCategory(org.jmhome.blog.model.Category)
	 */
	@Override
	public void updateCategory(Category category) {
		// TODO Auto-generated method stub
		getJpaTemplate().merge(category);
	}

	/**
	 * @see org.jmhome.blog.service.CategoryService#deleteCategory(org.jmhome.blog.model.Category)
	 */
	@Override
	public void deleteCategory(Category category) {
		getJpaTemplate().remove(category);
	}

	/**
	 * @see org.jmhome.blog.service.CategoryService#deleteCategory(java.lang.String)
	 */
	@Override
	public void deleteCategory(String categoryCode) {
		Category category = getCategory(categoryCode);
		getJpaTemplate().remove(category);
	}

	/**
	 * @see org.jmhome.blog.service.CategoryService#getCategory(java.lang.String)
	 */
	@Override
	public Category getCategory(String categoryCode) {
		return getJpaTemplate().find(Category.class, categoryCode);
	}

	/**
	 * @see org.jmhome.blog.service.CategoryService#getAllCategories()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Category> getAllCategories() {
		return getJpaTemplate().findByNamedQuery("org.jmhome.blog.model.Category@Categories");
	}
}
